package com.woniu.controller;

import com.woniu.entity.Result;
import com.woniu.pojo.Coach;
import com.woniu.pojo.Goods;
import com.woniu.service.ICoachService;
import com.woniu.vo.CoachEditVo;
import com.woniu.vo.GoodsEditVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.woniu.entity.Result;
import com.woniu.pojo.Coach;
import com.woniu.service.ICoachService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Coach;
import com.woniu.service.ICoachService;
import com.woniu.vo.CoachSearchVo;
import com.woniu.vo.QueryCoachDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xie
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/coach")
@Api(tags = "教练管理")
public class CoachController extends BaseController{
    @Autowired
    private ICoachService service;

    @ApiOperation("查询所有")
    @GetMapping("/queryAll")
    public Result queryAll(){
        List<Coach> coachList = service.list();
        return toDataResult(coachList);
    }

    @ApiOperation("分页查询条件")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result queryByItemInPage(@RequestBody(required = false)Coach coach, @PathVariable long currentPage, @PathVariable long pageSize){
        PageResult result=service.queryByItemInPage(coach,currentPage,pageSize);
        return toDataResult(result);
    }

    @ApiOperation("添加")
    @PostMapping
    public Result add(@RequestBody Coach coach){
        boolean flag = service.save(coach);
        return toResult(flag);
    }

    @ApiOperation("添加标签和教练信息")
    @PostMapping("/{dynamicTags}")
    public Result addLabel(@RequestBody Coach coach,@PathVariable String[] dynamicTags){
        if (dynamicTags.length>0) {
            String label="";
            for (String dynamicTag : dynamicTags) {
                label=label+dynamicTag+",";
            }
            String labels=label.substring(0,label.length()-1);
            coach.setGoodAt(labels);
        }
        boolean flag = service.save(coach);
        return toResult(flag);
    }

    @ApiOperation("通过id查询教练详情")
    @GetMapping("/detail/{id}")
    public Result queryDetailById(@PathVariable String id){
        QueryCoachDetailVo vo = service.queryDetailById(id);
        return toDataResult(vo);
    }



    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable String id){
        Coach coach = service.getById(id);
        return toDataResult(coach);
    }

    @ApiOperation("通过id查询教练标签及信息")
    @GetMapping("/label/{id}")
    public Result queryLabelById(@PathVariable String id){
        Coach coach = service.getById(id);
        CoachEditVo vo = new CoachEditVo();
        String label="";
        label = coach.getGoodAt();
        String[] split = label.split(",");
        vo.setCoach(coach);
        vo.setDynamicTags(split);
        return toDataResult(vo);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result modify(@RequestBody Coach coach){
        boolean flag = service.updateById(coach);
        return toResult(flag);
    }

    @ApiOperation("修改标签和教练信息")
    @PutMapping("/{dynamicTags}")
    public Result modifyLabel(@RequestBody Coach coach,@PathVariable String[] dynamicTags){
        if (dynamicTags.length>0) {
            String label="";
            for (String dynamicTag : dynamicTags) {
                label=label+dynamicTag+",";
            }
            String labels=label.substring(0,label.length()-1);
            coach.setGoodAt(labels);
        }
        boolean flag = service.updateById(coach);
        return toResult(flag);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        boolean flag = service.removeById(id);
        return toResult(flag);
    }

    @ApiOperation("通过id查询教练名称")
    @GetMapping("/name")
    public Result queryName(){
        List<CoachSearchVo> coachSearchVoList = service.queryName();
        return toDataResult(coachSearchVoList);
    }

    @ApiOperation("根据场馆id查询")
    @GetMapping("/findByGym/{id}")
    public Result findByGym(@PathVariable String id){
        List<Coach> coachList = service.findByGym(id);
        return toDataResult(coachList);
    }
}
